/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainUserPage.java
 *
 * Created on 8-Nov-2011, 6:33:05 PM
 */

package gui;
import javax.swing.*;
import gui.*;
import java.lang.String;
import javax.swing.JList;
import java.util.ArrayList;
import javax.swing.table.*;
 
import Client.*;
/**
 *
 * @author Jeremy
 */
public class MainUserPage extends javax.swing.JDialog {

    Client mainclient;
    ArrayList<Item> itemList;

      // itemList.insertRow();

    /** Creates new form MainUserPage */
    public MainUserPage(java.awt.Frame parent, boolean modal, Client clientobj) {
        super(parent, modal);
        this.mainclient = clientobj;
        initComponents(); 
    }

   // list.JTable.c

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        SubmitBid = new javax.swing.JButton();
        Logout = new javax.swing.JButton();
        SubmitItem = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        minbid = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        desc = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        bidvalue = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        getitemlist = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JSeparator();
        jScrollPane3 = new javax.swing.JScrollPane();
        list1 = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        list2 = new javax.swing.JTable();
        getitemlist1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setText("Items for Auction");

        SubmitBid.setText("Submit Bid");
        SubmitBid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitBidActionPerformed(evt);
            }
        });

        Logout.setText("Log out ");
        Logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogoutActionPerformed(evt);
            }
        });

        SubmitItem.setText("Submit Item");
        SubmitItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitItemActionPerformed(evt);
            }
        });

        jLabel4.setText("Place an Item for auction");

        minbid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minbidActionPerformed(evt);
            }
        });

        jLabel6.setText("Description");

        jLabel7.setText("Minimum Bid");

        desc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descActionPerformed(evt);
            }
        });

        jLabel8.setText("Enter Bid Value");

        jLabel9.setText("Exit Auction");

        getitemlist.setText("Update List");
        getitemlist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getitemlistActionPerformed(evt);
            }
        });

        list1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "itemID", "Current Bid", "Description"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane3.setViewportView(list1);

        list2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "itemID", "Current Bid", "Description"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane4.setViewportView(list2);

        getitemlist1.setText("Update List");
        getitemlist1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getitemlist1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(Logout, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator4)
                        .addComponent(jLabel4)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(minbid, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(SubmitItem, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(getitemlist1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel2)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addGap(18, 18, 18)
                            .addComponent(desc))
                        .addComponent(jSeparator2)
                        .addComponent(jScrollPane4)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addGap(19, 19, 19)
                            .addComponent(bidvalue, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(SubmitBid, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(getitemlist, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane3)))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(minbid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SubmitItem)
                    .addComponent(getitemlist1))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(desc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(bidvalue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SubmitBid)
                    .addComponent(getitemlist))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(Logout, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SubmitItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitItemActionPerformed
    
        String miniumumbid = minbid.getText();
        String selection = desc.getText();
        if(selection.isEmpty()) {
             JOptionPane.showMessageDialog(this, "Selection Field is Empty");

        }
        else if (miniumumbid.isEmpty()) {
             JOptionPane.showMessageDialog(this, "The minimum bid Field is Empty");

        }
        mainclient.offerMessage(selection,miniumumbid);
   
}//GEN-LAST:event_SubmitItemActionPerformed

    private void minbidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minbidActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_minbidActionPerformed

    private void descActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_descActionPerformed

    private void SubmitBidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitBidActionPerformed

        float currentbid;
        int rowIndex = list2.getSelectedRow();
        //int colIndex = list1.getSelectedColumn();
       
        if (bidvalue.getText().isEmpty()) {
             JOptionPane.showMessageDialog(this, "The minimum bid Field is Empty");
        }

        currentbid =  Float.valueOf(list2.getValueAt(rowIndex, 1).toString());
        String id = list2.getValueAt(rowIndex,0).toString();
        String bid = bidvalue.getText();

        if(bid.isEmpty()) {
             JOptionPane.showMessageDialog(this, "Selection Field is Empty");

        }
       
         if (currentbid > Float.valueOf(bid) ) {

             JOptionPane.showMessageDialog(this, "You bid is lower than the current bid. It needs to be higher than the current bid in order to submit");
        }

        mainclient.sendBidMessage(bid,id);

    }//GEN-LAST:event_SubmitBidActionPerformed

    private void LogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogoutActionPerformed
        mainclient.sendDeRegister();        // TODO add your handling code here:
    }//GEN-LAST:event_LogoutActionPerformed

    private void getitemlistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getitemlistActionPerformed
        mainclient.getitemlist();
    }//GEN-LAST:event_getitemlistActionPerformed

private void getitemlist1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getitemlist1ActionPerformed
// TODO add your handling code here:
    System.out.println("Not Implemented yet");
    PopUp unreg = new PopUp();
    unreg.messagetouser = "This function has not yet been implemented";
    unreg.DisplayMessage();
    
}//GEN-LAST:event_getitemlist1ActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                MainUserPage dialog = new MainUserPage(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Logout;
    private javax.swing.JButton SubmitBid;
    private javax.swing.JButton SubmitItem;
    private javax.swing.JTextField bidvalue;
    private javax.swing.JTextField desc;
    private javax.swing.JButton getitemlist;
    private javax.swing.JButton getitemlist1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTable list1;
    private javax.swing.JTable list2;
    private javax.swing.JTextField minbid;
    // End of variables declaration//GEN-END:variables

    public void updateTable(ArrayList<Item> itemList) {

        System.out.println("list size --->"+itemList.size());
        int listSize = itemList.size();
        //TODO clear table before updating
        list2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [listSize][3],
            new String [] {
                "itemID", "Current Bid", "Description"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });

        //fill table
        int j = 0;
        for(Item k:itemList){
            list2.setValueAt(k.itemId,j,0);
            list2.setValueAt(k.currentBid,j,1);
            list2.setValueAt(k.description,j,2);
            j++;
        }
    }

    public void updateMyTable(ArrayList<Item> itemList) {

        System.out.println("list size --->"+itemList.size());
        int listSize = itemList.size();
        //TODO clear table before updating
        list1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [listSize][3],
            new String [] {
                "itemID", "Current Bid", "Description"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });

        //fill table
        int j = 0;
        for(Item k:itemList){
            list1.setValueAt(k.itemId,j,0);
            list1.setValueAt(k.currentBid,j,1);
            list1.setValueAt(k.description,j,2);
            j++;
        }

    }

}
